package com.zuoye.stream;

import com.oracle.net.Sdp;

import javax.xml.transform.Source;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/26  16:35
 * @Copyright
 */
public class Dome {
    public static void main(String[] args) {
        zuoye1();
        count();
        sorted();
        distinct();
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("张三", 23, "男", 96));
        students.add(new Student("李四", 33, "女", 82));
        students.add(new Student("王五", 43, "男", 65));
        students.add(new Student("赵六", 53, "男", 64));
        students.add(new Student("十一", 63, "女", 73));
        students.add(new Student("周日", 43, "男", 65));
        students.add(new Student("钱七", 53, "男", 64));
        students.add(new Student("孙二", 63, "女", 73));
        students.add(new Student("吴里", 43, "男", 65));
        students.add(new Student("政和", 53, "男", 64));
        students.add(new Student("冯十", 63, "女", 73));

        Stream<Student> stream = students.stream();
        stream.skip(5).forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println("student = " + student);
            }
        });
        stream.skip(5).forEach(student -> {
            System.out.println("student = " + student);
        });
        stream.skip(5).forEach(System.out::println);
    }

    private static void distinct() {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("张三", 23, "男", 96));
        students.add(new Student("李四", 33, "女", 82));
        students.add(new Student("王五", 43, "男", 65));
        students.add(new Student("赵六", 53, "男", 64));
        students.add(new Student("十一", 63, "女", 73));
        students.add(new Student("周日", 43, "男", 65));
        students.add(new Student("钱七", 53, "男", 64));
        students.add(new Student("孙二", 63, "女", 73));
        students.add(new Student("吴里", 43, "男", 65));
        students.add(new Student("政和", 53, "男", 64));
        students.add(new Student("冯十", 63, "女", 73));

        Stream<Student> stream = students.stream();
        stream.distinct().forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println("student = " + student);
            }
        });
        stream.distinct().forEach(student -> System.out.println("student :" + student.getScore()));

        stream.distinct().forEach(System.out::println);
    }

    private static void sorted() {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("张三", 23, "男", 96));
        students.add(new Student("李四", 33, "女", 82));
        students.add(new Student("王五", 43, "男", 65));
        students.add(new Student("赵六", 53, "男", 64));
        students.add(new Student("十一", 63, "女", 73));
        students.add(new Student("周日", 43, "男", 65));
        students.add(new Student("钱七", 53, "男", 64));
        students.add(new Student("孙二", 63, "女", 73));
        students.add(new Student("吴里", 43, "男", 65));
        students.add(new Student("政和", 53, "男", 64));
        students.add(new Student("冯十", 63, "女", 73));

        Stream<Student> stream = students.stream();
        stream.sorted(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getScore() - o2.getScore();
            }
        });
        for (Student student : students) {
            System.out.println("student = " + student.getScore());
        }
        stream.sorted((o1, o2) -> o1.getScore() - o2.getScore()).forEach(student -> System.out.println("student = " + student));
        stream.sorted(Comparator.comparing(Student::getScore)).forEach(System.out::println);
    }

    private static void count() {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("张三", 23, "男", 96));
        students.add(new Student("李四", 33, "女", 82));
        students.add(new Student("王五", 43, "男", 65));
        students.add(new Student("赵六", 53, "男", 64));
        students.add(new Student("十一", 63, "女", 73));
        students.add(new Student("周日", 43, "男", 65));
        students.add(new Student("钱七", 53, "男", 64));
        students.add(new Student("孙二", 63, "女", 73));
        students.add(new Student("吴里", 43, "男", 65));
        students.add(new Student("政和", 53, "男", 64));
        students.add(new Student("冯十", 63, "女", 73));

        Stream<Student> stream = students.stream();

        System.out.println("stream = " + stream.count());
        Supplier<Long> runnable = () -> {
            return stream.count();
        };
        System.out.println("runnable = " + runnable.get().toString());
        Supplier<Long> count = stream::count;
        System.out.println("count = " + count.get().toString());

    }

    private static void zuoye1() {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("张三", 23, "男", 96));
        students.add(new Student("李四", 33, "女", 82));
        students.add(new Student("王五", 43, "男", 65));
        students.add(new Student("赵六", 53, "男", 64));
        students.add(new Student("十一", 63, "女", 73));
        students.add(new Student("周日", 43, "男", 65));
        students.add(new Student("钱七", 53, "男", 64));
        students.add(new Student("孙二", 63, "女", 73));
        students.add(new Student("吴里", 43, "男", 65));
        students.add(new Student("政和", 53, "男", 64));
        students.add(new Student("冯十", 63, "女", 73));

        Stream<Student> stream = students.stream();
        stream.limit(2).forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println("student = " + student.getName());
            }
        });
        stream.limit(2).forEach(student -> {
            System.out.println("student = " + student.getName());
        });
        stream.limit(6).forEach(System.out::println);
    }
}
